package com.hippy.erya.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.mapper.RawMaterialDictionaryMapper;
import com.hippy.erya.query.BaseQueryWrapper;
import com.hippy.erya.sys.core.constant.cache.Cache;
;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RawMaterialDictionaryServiceImpl extends ServiceImpl<RawMaterialDictionaryMapper, RawMaterialDictionaryDO> {

    @Autowired
    private RawMaterialDictionaryMapper rawMaterialDictionaryMapper;

    public Page<RawMaterialDictionaryDO> queryByPage(long currentPage, int limit, String name) {
        Page<RawMaterialDictionaryDO> page = new Page<>(currentPage, limit);
        QueryWrapper<RawMaterialDictionaryDO> queryWrapper = BaseQueryWrapper.buildQuery();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("NAME", name);
        }
        Page<RawMaterialDictionaryDO> pageResult = rawMaterialDictionaryMapper.selectPage(page, queryWrapper);
        pageResult.getRecords().forEach(BaseQueryWrapper::outputMessage);
        return pageResult;
    }

    public List<RawMaterialDictionaryDO> queryByCode(String code) {
        QueryWrapper<RawMaterialDictionaryDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("CODE", code);
        return rawMaterialDictionaryMapper.selectList(queryWrapper);
    }

    public List<RawMaterialDictionaryDO> queryByName(String name) {
        QueryWrapper<RawMaterialDictionaryDO> queryWrapper = BaseQueryWrapper.buildQuery();
        queryWrapper.eq("NAME", name);
        return rawMaterialDictionaryMapper.selectList(queryWrapper);
    }

    public Map<String, String> codeToName(String userCode) {
        QueryWrapper<RawMaterialDictionaryDO> wrapper = new QueryWrapper<>();
        wrapper.eq("USABLE_FLAG", Cache.USABLE);
        wrapper.orderByDesc("id");
        if (userCode == null) {
            userCode = String.valueOf(LoginContextHolder.getContext().getUser().getId());
        }
        wrapper.eq("USER_CODE", userCode);
        List<RawMaterialDictionaryDO> rawMaterialDictionaryDOS = rawMaterialDictionaryMapper.selectList(wrapper);
        Map<String, String> map = new HashMap<>();
        rawMaterialDictionaryDOS.forEach(rawMaterialDictionaryDO ->
                map.computeIfAbsent(rawMaterialDictionaryDO.getCode(), k -> rawMaterialDictionaryDO.getName()));
        return map;
    }


    public Map<String, String> nameToCode(String userCode) {
        QueryWrapper<RawMaterialDictionaryDO> wrapper = new QueryWrapper<>();
        wrapper.eq("USABLE_FLAG", Cache.USABLE);
        wrapper.orderByDesc("id");
        if (userCode == null) {
            userCode = String.valueOf(LoginContextHolder.getContext().getUser().getId());
        }
        wrapper.eq("USER_CODE", userCode);
        List<RawMaterialDictionaryDO> rawMaterialDictionaryDOS = rawMaterialDictionaryMapper.selectList(wrapper);
        Map<String, String> map = new HashMap<>();
        rawMaterialDictionaryDOS.forEach(rawMaterialDictionaryDO ->
                map.computeIfAbsent(rawMaterialDictionaryDO.getName(), k -> rawMaterialDictionaryDO.getCode()));
        return map;
    }

}
